package com.example.controller;



import com.example.dao.UserinfoMapper;
import com.example.pojo.Userinfo;
import com.example.util.ConstantUtil;
import com.example.util.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前端控制器
 */
@Controller
public class FrontController extends BaseController {

    @Resource
    UserinfoMapper userinfoMapper;






    /**
     * 跳转登陆页
     *
     * @param model
     * @return
     */
    @RequestMapping("pc/login")
    public String login(Model model) {
        return "pc/login";
    }

    /**
     * 退出登录
     *
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("pc/loginOut")
    public String loginOut(HttpServletRequest request, Integer id, Model model) {
        HttpSession session = request.getSession();
        session.removeAttribute("loginUserinfo");
        return "login";
    }

    /**
     * 登录提交
     *
     * @param username
     * @param password
     * @param vercode
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("pc/loginSubmit")
    public ResultUtil loginSubmit(String username, String password, String vercode, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (StringUtils.isBlank(vercode)) {
            return ResultUtil.error("验证码不能为空");
        }
        String randCode = session.getAttribute("randCode").toString();
        if (!randCode.equals(vercode)) {
            return ResultUtil.error("验证码不正确");
        }
        if (StringUtils.isBlank(username)) {
            return ResultUtil.error("用户名不能为空");
        }
        if (StringUtils.isBlank(password)) {
            return ResultUtil.error("登录密码不能为空");
        }
        Map map = new HashMap();
        map.put("name", username);
        List<Userinfo> beanList = userinfoMapper.selectAll(map);
        if (beanList.size() > 0) {
            Userinfo userinfo = beanList.get(0);
            if (userinfo.getPassword().equals(password)) {
                session.setAttribute("loginUserinfo", userinfo);
                session.setAttribute("loginUserinfoID", userinfo.getId());
                // 设置用户信息到Session作用域
                session.setAttribute(ConstantUtil.MANAGE_LOGIN_SERSSION, userinfo);
                session.setAttribute("loginuserName", username);
                //设置用户名
                session.setAttribute(ConstantUtil.MANAGE_LOGIN_SERSSION_ID, userinfo.getId());
                return ResultUtil.ok("登录成功");
            } else {
                return ResultUtil.error("密码错误");
            }
        } else {
            return ResultUtil.error("用户名错误");
        }
    }

    @RequestMapping("pc/register")
    public String reg() {
        return "register";
    }

    /**
     * 进入后台首页
     *
     * @return
     */
    @RequestMapping("/manageIndex")
    public String manageIndex() {
        return "index";
    }

    /**
     * 跳转到个人中心页面
     */
    @RequestMapping("manage/personalData")
    public String personalData(Model model, HttpSession session) {
        Integer adminId = (Integer) session.getAttribute(ConstantUtil.MANAGE_LOGIN_SERSSION_ID);
        return "personalData";
    }

    @RequestMapping("manage/userinfoCenter")
    public String userinfoCenter1(Model model, HttpSession session) {
        int userInfoID = getLoginUserInfoID(session);
        Userinfo userinfo = userinfoMapper.selectUserinfoById(userInfoID);
        model.addAttribute("userinfo", userinfo);
        return "userinfoCenter";
    }



//    获取前端登录用户ID
    public int getLoginUserInfoID(HttpSession session) {
        int userID = (int) session.getAttribute("loginUserinfoID");
        return userID;
    }
    public Userinfo getLoginUserInfo(HttpSession session) {
        int userID = (int) session.getAttribute("loginUserinfoID");
        Userinfo userinfo = userinfoMapper.selectUserinfoById(userID);
        return userinfo;
    }

//      更新个人信息

    @RequestMapping("pc/updateUserinfo")
    @ResponseBody
    public ResultUtil updateUserinfo(Userinfo userinfo, HttpSession session) {
        Date nowTime = new Date();
        userinfo.setCreatetime(nowTime);
        try {
            userinfoMapper.updateUserinfo(userinfo);
            userinfo = getLoginUserInfo(session);
            session.setAttribute("loginUserinfo", userinfo);
            return ResultUtil.ok("修改用户信息成功");
        } catch (Exception e) {
            return ResultUtil.error("修改用户信息出错,稍后再试！");
        }
    }




    @ResponseBody
    @RequestMapping("pc/registerSubmit")
    public ResultUtil regSubmit(HttpServletRequest request, Userinfo userinfo, String vercode) {
        HttpSession session = request.getSession();
        if (StringUtils.isBlank(vercode)) {
            return ResultUtil.error("验证码不能为空");
        }
        String randCode = session.getAttribute("randCode").toString();
        if (!randCode.equals(vercode)) {
            return ResultUtil.error("验证码不正确");
        }
        if (StringUtils.isBlank(userinfo.getName())) {
            return ResultUtil.error("姓名不能为空");
        }
        if (StringUtils.isBlank(userinfo.getPassword())) {
            return ResultUtil.error("登录密码不能为空");
        }
        userinfo.setRole("学生");
        userinfo.setCreatetime(new Date());
        userinfoMapper.insertUserinfo(userinfo);
        return ResultUtil.ok("注册成功");
    }

    /**
     * 用户个人中心
     *
     * @param model
     * @param session
     * @return
     */
  /*  @RequestMapping("pc/userinfoCenter")
    public String userinfoCenter(Model model, HttpSession session) {
        int userInfoID = getLoginUserInfoID(session);
        Userinfo userinfo = userinfoMapper.selectUserinfoById(userInfoID);
        model.addAttribute("userinfo", userinfo);
        return "/userinfoCenter";
    }

    @RequestMapping("manage/userinfoCenter")
    public String userinfoCenter1(Model model, HttpSession session) {
        int userInfoID = getLoginUserInfoID(session);
        Userinfo userinfo = userinfoMapper.selectUserinfoById(userInfoID);
        model.addAttribute("userinfo", userinfo);
        return "manage/userinfo/userinfoCenter";
    }





    *//**
     * 更新个人信息
     *//*
    @RequestMapping("pc/updateUserinfo")
    @ResponseBody
    public ResultUtil updateUserinfo(Userinfo userinfo, HttpSession session) {
        Date nowTime = new Date();
        userinfo.setCreatetime(nowTime);
        try {
            userinfoMapper.updateUserinfo(userinfo);
            userinfo = getLoginUserInfo(session);
            session.setAttribute("loginUserinfo", userinfo);
            return ResultUtil.ok("修改用户信息成功");
        } catch (Exception e) {
            return ResultUtil.error("修改用户信息出错,稍后再试！");
        }
    }

    *//**
     * 获取前端登录用户ID
     *
     * @param session
     * @return
     *//*
    public int getLoginUserInfoID(HttpSession session) {
        int userID = (int) session.getAttribute("loginUserinfoID");
        return userID;
    }

    public Userinfo getLoginUserInfo(HttpSession session) {
        int userID = (int) session.getAttribute("loginUserinfoID");
        Userinfo userinfo = userinfoMapper.selectUserinfoById(userID);
        return userinfo;
    }*/
}
